package com.kly.user.chatterService;

import com.alibaba.fastjson.JSONObject;

import java.util.Comparator;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 用于计算用户会话等级
 * @author linqu
 */
public class UserLevelHelper {

    /**
     * 用户发送消息数
     */
    private Integer msgNum;

    private String defaultUserChatLevelConfig;

    public Integer getUserLevel() {
        final List<UserLevelConfig> userLevelConfigs = JSONObject.parseArray(defaultUserChatLevelConfig, UserLevelConfig.class);
        final List<UserLevelConfig> sortedConfigs = userLevelConfigs.stream().sorted(Comparator.comparing(UserLevelConfig::getSortBy)).collect(Collectors.toList());

        for (UserLevelConfig config : sortedConfigs) {
            if (msgNum <= config.getValue()) {
                return Integer.valueOf(config.getLevelName());
            }
        }
        return 0;
    }

    public UserLevelHelper(Integer msgNum, String defaultUserChatLevelConfig) {

        this.msgNum = msgNum;
        this.defaultUserChatLevelConfig = defaultUserChatLevelConfig;
    }

    public Integer getMsgNum() {
        return msgNum;
    }

    public void setMsgNum(Integer msgNum) {
        this.msgNum = msgNum;
    }

    public String getDefaultUserChatLevelConfig() {
        return defaultUserChatLevelConfig;
    }

    public void setDefaultUserChatLevelConfig(String defaultUserChatLevelConfig) {
        this.defaultUserChatLevelConfig = defaultUserChatLevelConfig;
    }

}
